// pages/register/register.js
import {
  ApiRootUrl
} from '../../config/index.js'
import Toast from '../../@vant/weapp/toast/toast';
import {
  IMG_PATH
} from '../../config/index.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    IMG_PATH: IMG_PATH,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    username: '',
    phone: '',
    company: '请选择',
    identifycode: '',
    showcompany: false,
    columns: [],
    orgCode: '',
    code: "",
    name: "",
    type: "",
    canLogin:false,
    count:3,
  },
  usernameInput(event) {
    this.setData({
      username: event.detail
    })
  },
  identifycodeInput(event) {
    this.setData({
      identifycode: event.detail
    })
  },
  // 提交确认
  submitClick() {
    let self = this;
    let flag = true;
    if (!this.data.canLogin) {
      Toast('请先阅读隐私协议');
      return;
    }
    wx.checkSession({
      success: function (res) {
        console.log('session_key未过期')
        //session_key未过期
        flag = true;
      },
      fail: function (res) {
        flag = false;
        self.setData({
          tishiShow: true
        })
      }
    })
    if (!flag) {
      return;
    }
    if (!this.data.phone || !this.data.phone.length) {
      Toast('请输入手机号码！');
      return;
    }
    if (!this.data.identifycode || !this.data.identifycode.length) {
      Toast('请输入身份证号！');
      return;
    }
    if (!this.data.company || !this.data.company.length) {
      Toast('请选择面试企业！');
      return;
    }
    Toast.loading({
      duration: 0,
      mask: true,
      message: '操作中...',
      forbidClick: true,
    });
    let params = {
      name: this.data.username,
      phone: this.data.phone,
      identity: this.data.identifycode,
      componentType: this.data.type,
      name: this.data.username,
      originOrgCode:Number(this.data.type) == 0 ? this.data.code:"",
      originComponentName: Number(this.data.type) == 0 ?this.data.name:"",
      descOrgCode:Number(this.data.type) == 0 ? this.data.orgCode : this.data.code,
      descComponentName: Number(this.data.type) == 0 ?this.data.company : this.data.name,
      openId: wx.getStorageSync('openId')
    }
    console.log("params==>",params)
    wx.request({
      url: ApiRootUrl + '/index/registerEmployee',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      method: 'post',
      data: params,
      params: params,
      success(res) {
        Toast.clear();
        console.log("res==>",res)
        if (res.data.code == 20000) {
          console.log('res.data.data:', res.data.data);
          wx.setStorageSync('userinfo', res.data.data)
          wx.redirectTo({
            url: `../firstpage/firstpage?componentType=${self.data.type}&OrgCode=${self.data.code}&orgName=${self.data.name}&isLogin=${0}`,
          })
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        console.log("res==>",res)
        Toast.clear();
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络错误',
        });
      }
    })
  },
  // 公司选择确认
  companyConfirm(event) {
    console.log(event)
    this.setData({
      showcompany: false,
      company: event.detail.value.text,
      orgCode: event.detail.value.id,
    })
  },
  companyClose() {
    this.setData({
      showcompany: false
    })
  },
  // 公司选择取消
  companyCancel() {
    this.setData({
      showcompany: false
    })
  },
  companyClick(event) {
    this.setData({
      showcompany: true
    })
    console.log(event)
  },
  toOpen(){
    this.timer && clearInterval(this.timer)
    
    this.timer = setInterval(() => {
      let count = this.data.count
      if(count <= 0){
        clearInterval(this.timer)
        this.setData({
          count:count
        })
      }else{
        this.setData({
          count:count - 1
        })
      }
    }, 1000);
    this.setData({
      show:true
    })
  },
  onClose(){
    this.timer && clearInterval(this.timer)
    this.setData({
      show:false
    })
  },
  setPrativy(){
    this.setData({
      canLogin:true,
      show:false
    })
  },
  // 初始化公司list
  initCompany() {
    let self = this;
    let orgCode = this.data.code;
    console.log("orgCode==>",orgCode)
    wx.request({
      url: `${ApiRootUrl}/labor/queryBusinessList/${orgCode}`,
      method: 'get',
      header: {
        'content-type': 'application/json; charset=utf-8',
        'cookie': wx.getStorageSync("sessionid")
      },
      success(res) {
        if (res.data.code == 20000) {
          let columns = [];
          res.data.data.forEach((item) => {
            columns.push({
              id: item.orgCode,
              text: item.companyName
            })
          })
          app.globalData.companyList = columns;
          self.setData({
            columns: columns
          })
          console.log(res.data.data);
        } else {
          Toast({
            duration: 3000,
            forbidClick: true,
            type: 'fail',
            message: res.data.message,
          });
        }
      },
      fail(res) {
        Toast({
          duration: 3000,
          forbidClick: true,
          type: 'fail',
          message: '网络错误',
        });
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let phone = wx.getStorageSync("phoneNumber");
    this.setData({
      phone: phone,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})